package com.jpcc.vw.onlineRadio.adapter.item;

import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.action.OnlineRadioItemSelectedAction;
import com.jpcc.vw.onlineRadio.model.bean.LocalOnlineRadioEntity;
import com.jpcc.vw.onlineRadio.recycle.RecyclerView.BaseItem;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.widget.Indicator;

import de.greenrobot.event.EventBus;


/**
 * Created by jihongwen on 16/6/30.
 */

public class LocalOnlineRadioInfoItem extends BaseItem<LocalOnlineRadioEntity> implements View.OnClickListener {

    private LocalOnlineRadioEntity resultsBean;
    private TextView tv_item_count;
    private TextView tv_content2;
    private TextView tv_content3;
    private ImageView imv_delete;
    private ImageView imv_download;
    private RelativeLayout layout_top;
    private ImageView imv_radio_cover;
//    private Indicator imv_playing;
    private ImageView imv_play_status;

    public LocalOnlineRadioInfoItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_album_radio_item;
    }

    @Override
    public void onCreateView(View rootView) {
        tv_item_count = (TextView) rootView.findViewById(R.id.tv_item_count);
        tv_content2 = (TextView) rootView.findViewById(R.id.tv_content2);
        tv_content3 = (TextView) rootView.findViewById(R.id.tv_content3);
        imv_delete = (ImageView) rootView.findViewById(R.id.imv_delete);
        imv_download = (ImageView) rootView.findViewById(R.id.imv_delete);
        layout_top = (RelativeLayout) rootView.findViewById(R.id.layout_top);
        imv_radio_cover=(ImageView)rootView.findViewById(R.id.imv_radio_cover);
//        imv_playing=(Indicator)rootView.findViewById(R.id.imv_playing);
        imv_play_status=(ImageView)rootView.findViewById(R.id.imv_play_status);
    }

    @Override
    public void onBindView(final LocalOnlineRadioEntity response) {
        this.resultsBean = response;
        tv_item_count.setText(getPosition()+1 + "");
        tv_content2.setText(resultsBean.getName());




        String bgColor = "#404040";
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(getContext()).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(getContext()).getHmiAtmosphereLampColor();
                if (atmosphereStatus) {
                    if (!TextUtils.isEmpty(color)) {
                        bgColor = color;
                    }
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }








        if(AlbumRadioAudioPlayer.get().getPlayMusic()==null){
            layout_top.setBackgroundColor(Color.parseColor("#000000"));
//            imv_playing.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
        }
        else{
            if (AlbumRadioAudioPlayer.get().getPlayMusic().getAlbumId() == resultsBean.getId()) {
//                layout_top.setBackgroundColor(Color.parseColor(bgColor));
                layout_top.setBackgroundColor(Color.parseColor("#000000"));
//                imv_playing.setVisibility(View.VISIBLE);
                imv_play_status.setVisibility(View.VISIBLE);
            }
            else{
                layout_top.setBackgroundColor(Color.parseColor("#000000"));
//                imv_playing.setVisibility(View.GONE);
                imv_play_status.setVisibility(View.GONE);
            }
        }

        if(AlbumRadioAudioPlayer.get().isPlaying()||AlbumRadioAudioPlayer.get().isPreparing()){
//            imv_playing.setIndicatorPause(true);
            imv_play_status.setImageResource(R.drawable.ic_play_in_list);
        }
        else{
//            imv_playing.setIndicatorPause(false);
            imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
        }

        imv_radio_cover.setVisibility(View.VISIBLE);
        Glide.with(getContext()).load(resultsBean.getSmallCoverUrl()).into(imv_radio_cover);
    }

    @Override
    public void onClick(View v) {
        OnlineRadioItemSelectedAction action = new OnlineRadioItemSelectedAction();
        action.setPosition(getPosition());
        EventBus.getDefault().post(action);
    }
}
